#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    //ui->openGLWidget->setFixedSize(800, 400); // 设置合适的大小
    inv = false;
    wave = 800;
    QFont font("微软雅黑", 8, QFont::Bold);
    ui->openGLWidget->initBuffer(wave, 500, 160.f)->setBackground(QColor(0, 0, 0));
    ui->openGLWidget->setX(8)->setXText("D:/code/demo7/1.png")
        ->setY(2)->setYText("振幅", font, QColor(0,255,0))
        ->setZ(8)->setZText("相位", font, QColor(0,0,255));
    ui->openGLWidget->setColorMax(80.0f)
        ->setDrawType(DrawVertical)
        ->setGridNum(5, 10, 5, 10)->setRotate(0, 10, 0);
    ui->openGLWidget->setColorType(COLOR_SELECTOR)
        ->setColorSelector(0.f, 10.f, 50, 0, 0)
        ->setColorSelector(10, 30, 0, 255, 0)
        ->setColorSelector(30, 50, 0, 0, 255)
        ->setColorSelector(50, 60, 255, 255, 255)
        ->setColorSelector(60, 70, 128, 128, 128)
        ->setColorSelector(70, 75, 0, 128, 215)
        ->setColorSelector(75, 100, 0, 255, 255);

    ui->openGLWidget_2->initBuffer(100, 1000, 80.f);
    ui->openGLWidget_2->setX(8)->setXText("时间", font, QColor(255, 0, 0))
        ->setY(2)->setYText("振幅", font, QColor(0,255,0))
        ->setZ(8)->setZText("相位", font, QColor(0,0,255));
    ui->openGLWidget_2->setColorMax(80.0f)->setDrawType(DrawLine);
    ui->openGLWidget_2->setColorType(COLOR_RANDOM)
        ->setColorSelector(0.f, 30.f, 50, 0, 0)
        ->setColorSelector(30, 60, 0, 255, 0)
        ->setColorSelector(60, 1000, 0, 0, 255);

    QTimer* timer = new QTimer(this);
    timer->setInterval(30);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
    timer->start();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateData()
{
    static float tt = 1.0f;
    static int a = 0;

    float arr[wave];

    for(int i= 0; i < wave; i++)
    {
        arr[i] = tt;
    }
    //qDebug() << a;
    // if (a < 5)
    // {
    //     ui->openGLWidget->setData(arr);
    // }
    // else if (a < 10)
    // {
    //     //qDebug() << "setSomeData";
    //     ui->openGLWidget->setSomeData(arr, 30);
    // }
    // else if (a < 30)
    // {
    //     ui->openGLWidget->setSomeData(arr, 10);
    // }
    // else
    {
        ui->openGLWidget->setData(arr);
    }
    a++;
    ui->openGLWidget_2->setData(arr);
    if (inv)
    {
        tt = tt - 1.0f;
    }
    else
    {
        tt = tt + 1.0f;
    }

    if (tt > 80.0f)
    {
        inv = true;
    }
    else if(tt <= 1.0f)
    {
        inv = false;
    }
}
